package com.ship.auth.controller;

import javax.servlet.http.HttpServletRequest;

import com.ship.common.core.web.domain.JsonResult;
import com.ship.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ship.auth.form.LoginBody;
import com.ship.auth.form.RegisterBody;
import com.ship.auth.service.SysLoginService;
import com.ship.common.core.domain.R;
import com.ship.common.core.utils.JwtUtils;
import com.ship.common.core.utils.StringUtils;
import com.ship.common.security.auth.AuthUtil;
import com.ship.common.security.service.TokenService;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.system.api.model.LoginUser;

import java.util.Objects;
import java.util.Random;

/**
 * token 控制
 * 
 * @author zhangyh
 */
@RestController
@Api(value = "authToken", tags = "用户登录")
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @PostMapping("login")
    @ApiOperation(value = "用户登录")
    public JsonResult login(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());

        return JsonResult.success(tokenService.createToken(userInfo));
    }

    @ApiOperation(value = "用户退出")
    @DeleteMapping("logout")
    public JsonResult logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return JsonResult.success();
    }

    @ApiOperation(value = "内网用户退出使用")
    @GetMapping("Internal/logout")
    public JsonResult internalLogout(HttpServletRequest request)
    {
        int randomState = new Random().nextInt(999901) + 100;
        String state = String.valueOf(randomState);
        String url = "";
        String token = SecurityUtils.getToken(request);
        System.out.println(token);
        if (StringUtils.isNotEmpty(token)) {
            if (token.contains(".")) {
                String username = JwtUtils.getUserName(token);
                // 删除用户缓存记录
                AuthUtil.logoutByToken(token);
                if(StringUtils.isNotEmpty(username)){
                    tokenService.delLoginUser(token);
                }
                // 记录用户退出日志
//            sysLoginService.logout(username);
                System.out.println("token中有点");
//            url = "https://devau.cnooc.com.cn/idp/oauth2/authorize?state=" + state + "&response_type=code&client_id=lngyscbdd&redirect_uri=https://10.75.34.224/api/workbench/test";
//            url = "https://au.cnooc.com.cn/idp/oauth2/authorize?state=" + state + "&response_type=code&client_id=lngyscbdd&redirect_uri=https://10.75.34.224/api/workbench&out=out";
            }else {
                System.out.println("token中无点");
//                url = "https://devau.cnooc.com.cn/idp/oauth2/authorize?state=" + state + "&response_type=code&client_id=lngyscbdd&redirect_uri=https://10.75.34.224/api/workbench";
//                url = "https://au.cnooc.com.cn/idp/oauth2/authorize?state=" + state + "&response_type=code&client_id=lngyscbdd&redirect_uri=https://10.75.34.224/api/workbench";
            }
        }else {
            System.out.println("token为空");
//            url = "https://devau.cnooc.com.cn/idp/oauth2/authorize?state="+state+"&response_type=code&client_id=lngyscbdd&redirect_uri=https://10.75.34.224/api/workbench";
//            url = "https://au.cnooc.com.cn/idp/oauth2/authorize?state="+state+"&response_type=code&client_id=lngyscbdd&redirect_uri=https://10.75.34.224/api/workbench";
        }
        return JsonResult.success(url);
    }
    @ApiOperation(value = "刷新token")
    @GetMapping("refresh")
    public JsonResult refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNull(loginUser))
        {
            return JsonResult.failed("登录用户信息异常！");
        }
        // 用户登录
        LoginUser userInfo = sysLoginService.refresh(loginUser.getUsername());
        return JsonResult.success(tokenService.createToken(userInfo));
    }

    @ApiOperation(value = "注册")
    @PostMapping("register")
    public JsonResult register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return JsonResult.success();
    }
}
